<?php

namespace app\common\model;

use Think\Db;

/**
 * 图片管理
 */
class PicLists extends Base {

    /**
     * 获取图片信息
     */
    public function getById($id) {
        return (Object) $this->where('id', $id)->find()->toArray();
    }

/**
 * 获取图片列表
 * @param type $where
 * @return type
 */
    public function getAll($where = null) {
        return $this->where($where)->order('id', 'asc')->select()->toArray();
    }

    /**
     * 删除图片
     */
    public function del() {
        $id = input('id');
        $result = $this->where("id={$id} or id={$id}")->delete();
        if (false !== $result) {
            return [1, "删除成功:" . input('id')];
        } else {
            return [0, $this->getError()];
        }
    }

    /**
     * 获取josn用到的列表
     */
    public function getPicList($type) {

        $page = input('get.page');
        $limit = input('get.limit');
        $where = '1=1';

        if (input('get.key/a')) {
            foreach (input('get.key/a') as $k => $v) {
                if (!empty($v)) {
                    switch ($k) {
                        case "name":
                            $where.=" and {$k} like '%{$v}%'";
                            break;
                        case "type_id":
                            $where.=" and {$k} like '%{$v}%'";
                            break;
                    }
                }
            }
        }
        if ($type == 1) {//分页
            return $this->where($where)->page($page, $limit)->select()->toArray();
        } else {//总数
            return $this->where($where)->select()->toArray();
        }
    }
}
